package basic;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;



public class Demo3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		if (args.length!=2) {
			System.err.println("user infos err: <inpath>,<outpath>");
			System.exit(-1);
		}
		@SuppressWarnings("deprecation")
		Job job = new Job(new Configuration(), "paixu");
		job.setJarByClass(Demo3.class);
		FileInputFormat.addInputPath(job, new Path(args[0]));//输入路径
		FileOutputFormat.setOutputPath(job,new Path(args[1]));//输出路径
		
		job.setMapperClass(smap.class);
		job.setReducerClass(sreduce.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		job.waitForCompletion(true);


	}
	public static class smap extends Mapper<LongWritable, Text, IntWritable, IntWritable>{
		 private static IntWritable d=new IntWritable();
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,IntWritable,IntWritable>.Context context) throws java.io.IOException ,InterruptedException {
			String line=value.toString();
			 d.set(Integer.parseInt(line));
             context.write(d, new IntWritable(1));
		};
	}
	public static class sreduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>{
		private static IntWritable linenum = new IntWritable(1);
		protected void reduce(IntWritable k2, java.lang.Iterable<IntWritable> values, org.apache.hadoop.mapreduce.Reducer<IntWritable,IntWritable,IntWritable,IntWritable>.Context context) throws java.io.IOException ,InterruptedException {
			for(IntWritable val:values){
                context.write(linenum, val);
                linenum = new IntWritable(linenum.get()+1);
            }
		};
		
	}

}
